/**
 * User: Célia Calçada
 * Date: 01-07-2013
 * Login app controller
 */

Ext.define('Mobile-app.controller.Login', {
    extend: 'Ext.app.Controller',

    config: {
        //add view references for this controller
        refs: {
            loginView: 'loginview',
            mainView: 'mainview'
        },

        //Definition of all listen events of this controller
        //can be triggered by the [fireEvent] method
        control: {

            //signInCommand triggered by the Login view
            //ref will be handled by the onSignInCommand method
            loginView: {
                signInCommand: 'onSignInCommand'
            },
            //signInCommand triggered by the Login view
            //ref will be handled by the onSignInCommand method
            mainView: {
                signOffCommand: 'onSignOffCommand'
            }

        }

    },

    /***
     * Used to slide this panel to the left (Animation)
     * @returns {{type: string, direction: string}}
     */
    getSlideLeftTransition: function () {
        return { type: 'slide', direction: 'left' };
    },

    /***
     * Used to slide this panel to the right (Animation)
     * @returns {{type: string, direction: string}}
     */
    getSlideRightTransition: function () {
        return { type: 'slide', direction: 'right' };
    },

    /***
     * Trigger event
     * Used to trigger the sign in algorithm
     * @param view The View reference where this event was triggered
     * @param username The username value
     * @param password  The password value
     */
    onSignInCommand: function (view, username, password) {

        //get view reference
        var loginView = this.getLoginView();

        //check if the user filled both fields
        if (username.length === 0 || password.length === 0) {
            loginView.showSignInFailedMessage($.t('error.1'));
            return;
        }

        //show loading mask and message
        loginView.setMasked({
            xtype: 'loadmask',
            message: $.t('main.loading').toLowerCase() + '...'
        });

        //check authentication with an AJAX request
        Ext.Ajax.request({

            url: 'app/php/public/user.auth.php',
            method: 'post',
            scope: this,
            params: {
                username: username,
                password: password
            },

            /***
             * Callback method
             * Called when the AJAX request is performed successfully
             * @param response The response object
             */
            success: function (response) {
                //hide the loading mask
                this.getLoginView().hideMask();

                try{

                    //get the JSON object response
                    var rst = Ext.JSON.decode(response.responseText);

                    //check if the authentication was performed successfully
                    if (rst.success) {

                        //reset fields
                        this.getLoginView().reset();

                        //slide of the current view and show the main view
                        Ext.Viewport.animateActiveItem(this.getMainView(), this.getSlideLeftTransition());

                        //save user object
                        Ext.app.user = rst.user;
                        Ext.app.events = rst.events;
                    }
                    else {
                        //otherwise, show the failure auth message
                        this.signInFailure($.t('error.' + rst.errorCode));
                    }

                } catch(e){
                    this.signInFailure($.t('error.5'));
                }
            },

            /***
             * Callback method
             * Called when the AJAX request is performed unsuccessfully
             * @param response The response object
             */
            failure: function (response) {
                this.getLoginView().hideMask();
                this.signInFailure($.t('error.6', {error: response.statusText}));
            }

        });

    },

    /***
     * Trigger event
     * Used to trigger the sign off algorithm
     */
    onSignOffCommand: function(){
        //show login view
        Ext.Viewport.animateActiveItem(this.getLoginView(), this.getSlideRightTransition());

        //reload page
        window.location = "?" + new Date().getTime();

    },

    /***
     * General function to warn use that the sign process was unsuccessful
     * @param message The message to be sent to the user
     */
    signInFailure: function (message) {

        //get login view
        var loginView = this.getLoginView();

        //show error message and hide the mask
        loginView.showSignInFailedMessage(message);
        loginView.setMasked(false);

    }

});